package com.leruan.aasimp.action.usedcar;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.leruan.aasimp.bean.UsedCarBuyMainBean;
import com.leruan.aasimp.bean.UsedCarSaleMainBean;
import com.leruan.aasimp.constant.UsedCarConstant;
import com.leruan.aasimp.entity.Eauser;
import com.leruan.aasimp.entity.UsedCarBuy;
import com.leruan.aasimp.service.UsedCarService;
import com.opensymphony.xwork2.ActionSupport;

public class ViewBuyUsedCar extends ActionSupport{

	private static final long serialVersionUID = -2698149721025002917L;

	@Resource
	private UsedCarService usedCarService;
	
	public String show(){
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			request.setAttribute("carBuyAddress", UsedCarConstant.carBuyAddress);
			request.setAttribute("priceList", UsedCarConstant.priceList);
			
			int pageNo = 1;
			String req_no = request.getParameter("no");
			if(req_no != null)
				pageNo = Integer.parseInt(req_no);
		
			request.setAttribute("isAddress", 0);
			String req_address = request.getParameter("address");
			if(req_address != null & !"".equals(req_address)){
				String address = new String(req_address.getBytes("iso-8859-1"), "UTF-8");
				map.put("address", address);
				request.setAttribute("address1", address);
				request.setAttribute("isAddress", 1);
			}
			
			request.setAttribute("isPrice", 0);
			String req_price = request.getParameter("price");
			if(req_price != null & !"".equals(req_price)){
				String price = new String(req_price.getBytes("iso-8859-1"), "UTF-8");
				map = UsedCarConstant.convertPrice_2(map, price);
				request.setAttribute("price1", price);
				request.setAttribute("isPrice", 1);
			}
			
			List<UsedCarBuyMainBean> usedCarList = usedCarService.getUsedCarBuyList(map, pageNo);
			Long total = usedCarService.getUsedBuyTotal(map);
			request.setAttribute("usedCarBuyList", usedCarList);
			request.setAttribute("pageNoBuy", pageNo);
			request.setAttribute("pageNoCenterBuy", 3);
			Long totalNo = total / UsedCarConstant.PAGE_SIZE_2 + 1;
			request.setAttribute("totalNoBuy", totalNo);
			request.setAttribute("totalBuy", total);
			if (pageNo < 3 || totalNo < 5) {
				request.setAttribute("pageNoCenterBuy", 3);
			} else if (pageNo > totalNo - 2) {
				request.setAttribute("pageNoCenterBuy", totalNo - 2);
			} else {
				request.setAttribute("pageNoCenterBuy", pageNo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "show";
	}
	
	public String showBuyInfo(){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		request.setAttribute("id", request.getParameter("id"));
		return "showInfo";
	}
	
	public String showDetail(){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");
		try {
			UsedCarBuy usedCarBuy = usedCarService.getUsedCarBuyById(id);
			request.setAttribute("usedCarBuy", usedCarBuy);
			
			usedCarBuy.setBrowseNum(usedCarBuy.getBrowseNum() + 1);
			
			usedCarService.updateUsedCarBuy(usedCarBuy);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "showDetail";
		
	}
	public String showMyUsedCarBuy(){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		
		try {
			Eauser eauser = (Eauser) session.getAttribute("eauser");
			List<UsedCarBuyMainBean> usedCarList = usedCarService.getUsedCarBuyListById(eauser.getUserId());
			request.setAttribute("usedCarBuyList", usedCarList);
			if(usedCarList.size() == 0)
				request.setAttribute("size", 0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "myUsedCarBuyInfo";
	}
	
	public String deleteMyUsedCarBuy(){
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		String id = request.getParameter("id");
		
		Eauser eauser = (Eauser) session.getAttribute("eauser");
		try {
			usedCarService.deleteMyUsedCarBuy(id, eauser.getUserId());
			request.setAttribute("isDelete", 1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return showMyUsedCarBuy();
	}
	
	public String add(){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		HttpSession session = request.getSession();
		String title = request.getParameter("title");
		String gearbox = request.getParameter("gearbox");
		String dispalcementSize = request.getParameter("dispalcementSize");
		String mileageSize = request.getParameter("mileageSize");
		String money = request.getParameter("money");
		String address = request.getParameter("address");
		String content = request.getParameter("content"); 
		String telephone = request.getParameter("telephone");  
		String name = request.getParameter("name");  
		
		Eauser eauser = (Eauser) session.getAttribute("eauser");
		Map <String, String> map = new HashMap<String, String>();
		map.put("title", title);
		map.put("gearbox", gearbox);
		map.put("dispalcementSize", dispalcementSize);
		map.put("mileageSize", mileageSize);
		map.put("money", money);
		map.put("address", address);
		map.put("content", content);
		map.put("telephone", telephone);
		map.put("name", name);
		map.put("userId", eauser.getUserId());
		try {
			usedCarService.saveUsedCarBuy(map);
			
			request.setAttribute("add", 1);
			
			request.setAttribute("carBuyAddress", UsedCarConstant.carBuyAddress);
			request.setAttribute("priceList", UsedCarConstant.priceList);
			int pageNo = 1;
			request.setAttribute("isAddress", 0);
			Map<String, Object> map1 = new HashMap<String, Object>();
			List<UsedCarBuyMainBean> usedCarList = usedCarService.getUsedCarBuyList(map1, pageNo);
			Long total = usedCarService.getUsedBuyTotal(map1);
			request.setAttribute("usedCarBuyList", usedCarList);
			request.setAttribute("pageNoBuy", pageNo);
			request.setAttribute("pageNoCenterBuy", 3);
			Long totalNo = total / UsedCarConstant.PAGE_SIZE_2 + 1;
			request.setAttribute("totalNoBuy", totalNo);
			request.setAttribute("totalBuy", total);
			if (pageNo < 3 || totalNo < 5) {
				request.setAttribute("pageNoCenterBuy", 3);
			} else if (pageNo > totalNo - 2) {
				request.setAttribute("pageNoCenterBuy", totalNo - 2);
			} else {
				request.setAttribute("pageNoCenterBuy", pageNo);
			}
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "show";
	}
	
	
	public void setUsedCarService(UsedCarService usedCarService) {
		this.usedCarService = usedCarService;
	}
	
}
